package san.java.day14;

import java.lang.reflect.Field;

/**
 * 获取所有的属性
 *
 * 获取类所在的包
 */

public class Reflect_07 {

    public static void main(String[] args) {
        try {
            testGetFieldsByReflect();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            testGetPackageByReflect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testGetPackageByReflect() throws Exception {
        Class<?> dogClass = Class.forName("san.java.day14.GoldenDog");
        Package dogPackage = dogClass.getPackage();
        System.out.println(dogPackage.getName());
    }

    public static void testGetFieldsByReflect() throws Exception {
        Class<?> dogClass = Class.forName("san.java.day14.GoldenDog");

        // 1. 获取所有公有属性 (包含父类的共有属性)
        Field[] fields = dogClass.getFields();
        for (Field f: fields) {
            System.out.println("修饰符: " + f.getModifiers());
            System.out.println("属性类型: " + f.getType());
            System.out.println("属性名: " + f.getName());

            System.out.println("==========================");
        }

        // 2. 获取所有属性 (不包含父类的属性)
        Field[] afields = dogClass.getDeclaredFields();
        for (Field f: afields) {
            System.out.println("修饰符: " + f.getModifiers());
            System.out.println("属性类型: " + f.getType());
            System.out.println("属性名: " + f.getName());

            System.out.println("==========================");
        }

    }
}
